package net.lainiao.leetcode;

import com.fasterxml.jackson.databind.node.BigIntegerNode;

public class LintCode6 {
    public static void main(String[] args) {
        int[] A={1,2,3,4};
        int[] B={2,4,5,6};
        LintCode6 code6=new LintCode6();
        int[] C=code6.mergeSortTwoArray(A,B);
        for (int i = 0; i <C.length ; i++) {
            System.out.println(C[i]);
        }
    }

    private int[] mergeSortTwoArray(int[] a, int[] b) {
        int lenA=a.length;
        int lenB=b.length;
        if(lenB>lenA){
            return mergeSortTwoArrayInfo(b,a);
        }
        return mergeSortTwoArrayInfo(a,b);
    }

    private int[] mergeSortTwoArrayInfo(int[] a, int[] b) {
        int lenA=a.length;
        int lenB=b.length;
        int len=a.length+b.length;
        int[] res=new int[len];
        int currentIndex=0;
        int bIndex=0;
        int aIndex=0;
        while(currentIndex<len&&aIndex<lenA&&bIndex<lenB){
            int vA=a[aIndex];
            int vB=b[bIndex];
            if(vA<vB){
                res[currentIndex]=vA;
                aIndex++;
            }
            else{
                res[currentIndex]=vB;
                bIndex++;
            }
            currentIndex++;
        }

        if(aIndex<lenA){
            for (int i = aIndex; i <lenA ; i++) {
                res[currentIndex]=a[i];
                currentIndex++;
            }
        }

        if(bIndex<lenB){
            for (int i = bIndex; i <lenB ; i++) {
                res[currentIndex]=b[i];
                currentIndex++;
            }
        }

        return res;
    }
}
